import 'package:ai_data_capture/bean/process_enum.dart';
import 'package:ai_data_capture/main.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

import 'logic.dart';

class MainPage extends StatelessWidget {
  const MainPage({super.key});

  @override
  Widget build(BuildContext context) {
    final MainLogic logic = Get.find<MainLogic>();
    final state = logic.state;
    return Scaffold(
        appBar: AppBar(title: const Text("AI数据提取终端"), actions: [
          InkWell(
              onTap: logic.init,
              child: const Padding(
                  padding: EdgeInsets.all(8.0), child: Icon(Icons.refresh)))
        ]),
        body: Padding(
            padding: const EdgeInsets.all(10),
            child: SingleChildScrollView(
                child: Column(children: [
              Row(children: [
                Expanded(
                    child: Container(
                        decoration: BoxDecoration(color: Colors.grey.shade100),
                        child: TextField(
                            controller: logic.controller,
                            decoration: const InputDecoration(
                                contentPadding: EdgeInsets.symmetric(
                                    horizontal: 10, vertical: 10),
                                hintText: "请输入链接",
                                isCollapsed: true,
                                isDense: true,
                                border: InputBorder.none)))),
                InkWell(
                    onTap: logic.setup,
                    child: Container(
                        decoration: BoxDecoration(
                            color: Colors.blue,
                            borderRadius: BorderRadius.circular(5)),
                        padding: const EdgeInsets.all(8.0),
                        child: const Text("设置",
                            style: TextStyle(color: Colors.white))))
              ]),
              const SizedBox(height: 10),
              InkWell(
                  onTap: () {
                    Get.toNamed(RoutePath.configfile);
                  },
                  child: Container(
                      padding: const EdgeInsets.all(10),
                      width: double.infinity,
                      decoration: BoxDecoration(
                          color: Colors.cyan.shade100,
                          borderRadius: BorderRadius.circular(10)),
                      child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            Padding(
                              padding: const EdgeInsets.all(5),
                              child: Obx(() {
                                return Text(
                                    "配置id：${state.currentConfig.value?.config_id ?? ''}",
                                    style: const TextStyle(
                                        fontWeight: FontWeight.bold,
                                        fontSize: 20));
                              }),
                            ),
                            Padding(
                                padding: const EdgeInsets.all(8.0),
                                child: Obx(() {
                                  return Text(
                                      "配置描述：${state.currentConfig.value?.description ?? ""}");
                                }))
                          ]))),
              const SizedBox(height: 10),
              Container(
                  width: double.infinity,
                  padding: const EdgeInsets.all(10),
                  decoration: BoxDecoration(
                      color: Colors.blue.withAlpha(50),
                      borderRadius: BorderRadius.circular(10)),
                  child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        const Padding(
                            padding: EdgeInsets.all(5),
                            child: Text("当前状态",
                                style: TextStyle(
                                    fontWeight: FontWeight.bold,
                                    fontSize: 18))),
                        Obx(() {
                          return Text(
                              "是否正在运行:${state.status.value?.isRunning ?? false}");
                        }),
                        Obx(() {
                          return Text(
                              "异常信息:${state.status.value?.error ?? '(无)'}");
                        }),
                        Obx(() {
                          return Text(
                              "运行状态:${getStatus(state.status.value?.status)}");
                        }),
                        Obx(() {
                          return Text(
                              "当前进度:${double.tryParse('${state.status.value?.process ?? 0}') ?? 0}%");
                        }),
                        Obx(() {
                          return Text(
                              "当前程序:${ProcessEnum.values.firstWhereOrNull((m) => "${m.value}" == "${state.status.value?.procedure}")?.desc ?? '(无)'}");
                        }),
                        Obx(() {
                          return Text(
                              "开始时间:${state.status.value?.execStartTime ?? '(无)'}");
                        }),
                        Obx(() {
                          return Text(
                              "结束时间:${state.status.value?.execEndTime ?? '(无)'}");
                        })
                      ])),
              const SizedBox(height: 10),
              Container(
                  width: double.infinity,
                  padding: const EdgeInsets.all(10),
                  decoration: BoxDecoration(
                      color: Colors.blue.withAlpha(50),
                      borderRadius: BorderRadius.circular(10)),
                  child: Obx(() {
                    if (state.status.value?.fileList == null ||
                        state.status.value?.fileList?.isEmpty == true) {
                      return const Text("暂无文件");
                    }
                    return Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Padding(
                            padding: const EdgeInsets.all(8.0),
                            child: Text(
                              "文件(${state.status.value?.fileList?.length ?? 0})",
                              style:
                                  const TextStyle(fontWeight: FontWeight.bold),
                            ),
                          ),
                          ListView.builder(
                              shrinkWrap: true,
                              physics: const NeverScrollableScrollPhysics(),
                              itemCount: state.status.value!.fileList!.length,
                              itemBuilder: (BuildContext context, int index) {
                                var file = state.status.value!.fileList![index];
                                return Container(
                                    padding: const EdgeInsets.all(10),
                                    decoration: BoxDecoration(
                                        color: Colors.lightBlueAccent
                                            .withAlpha(50),
                                        borderRadius:
                                            BorderRadius.circular(10)),
                                    child: Column(
                                        crossAxisAlignment:
                                            CrossAxisAlignment.start,
                                        children: [
                                          Text("${file.fileName}",
                                              style: const TextStyle(
                                                  fontWeight: FontWeight.bold)),
                                          Text("所在路径：${file.filePath}"),
                                          Text("文件大小：${file.fileSize}"),
                                          Text("数据数量：${file.count}"),
                                          Text("生成时间：${file.fileCreatedAt}"),
                                        ]));
                              }),
                          Obx(() {
                            if (state.status.value?.zipPath == null ||
                                state.status.value?.zipSize == null) {
                              return Container();
                            }
                            return Container(
                                margin:
                                    const EdgeInsets.symmetric(vertical: 10),
                                decoration: BoxDecoration(
                                    color: Colors.lightBlueAccent.withAlpha(50),
                                    borderRadius: BorderRadius.circular(10)),
                                padding: const EdgeInsets.all(10),
                                child: Column(
                                    crossAxisAlignment:
                                        CrossAxisAlignment.start,
                                    children: [
                                      Text(
                                          "压缩文件；路径：${state.status.value?.zipPath}"),
                                      Text(
                                          "压缩文件大小：${state.status.value?.zipSize}")
                                    ]));
                          })
                        ]);
                  })),
              const SizedBox(height: 10),
              Row(
                children: [
                  Expanded(
                    child: ElevatedButton(
                        onPressed: () {
                          Get.toNamed(RoutePath.taskList);
                        },
                        child: const Text("任务列表")),
                  ),
                  Expanded(
                    child: ElevatedButton(
                        onPressed: () {
                          Get.toNamed(RoutePath.flowList);
                        },
                        child: const Text("流程列表")),
                  ),
                  Expanded(
                    child: ElevatedButton(
                        onPressed: () {
                          Get.toNamed(RoutePath.programList);
                        },
                        child: const Text("程序列表")),
                  ),
                ],
              ),
              const SizedBox(height: 10),
            ]))));
  }

  String getStatus(num? status) {
    if (status == 0) {
      return "进行中";
    }
    if (status == 1) {
      return "已完成";
    }
    return "运行错误";
  }
}
